package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("开始处理登录业务，参数：{}", userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if(userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("保存用户业务，参数：{}", userSaveParam);
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if (user.getId()==null){//新增操作
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else {
            //TODO：修改操作
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }

    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("查询用户业务，参数：{}", userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        return list;
    }

    @Override
    public void resetPassword(Long id) {
        log.debug("重置密码业务，参数：{}", id);
        User user = new User();
        user.setId(id);
        user.setPassword("root");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("更新用户状态业务，参数：{},{}", id, status);
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long id) {
        log.debug("删除用户业务，参数：{}", id);
        userMapper.delete(id);
    }

    @Override
    public List<UserVO> selectAuditUser(Long parentId) {
        /*log.debug("查询审核用户业务，参数：{}", parentId);
        ArrayList<UserVO> AuditUserList = new ArrayList<>();
        UserQuery userQuery = new UserQuery();
        while (true){
            userQuery.setId(parentId);
            List<UserVO> list = userMapper.selectUser(userQuery);
            if(parentId!=null){
                AuditUserList.addAll(list);
                parentId = list.get(list.size()-1).getParentId();
            }else{
                return AuditUserList;
            }
        }*/
        log.debug("查询审核用户业务，参数：{}", parentId);
        //1.准备一个空集合,用来保存查出来的多个审批人
        ArrayList<UserVO> userVOList = new ArrayList<>();
        //2.先根据传过来的直属id将直属领导先查出来,装到上方集合中
        UserVO auditUSer1 = userMapper.selectById(parentId);
        userVOList.add(auditUSer1);
        //3.如果有直属领导,且直属领导还有上级
        if(auditUSer1 != null && auditUSer1.getParentId()!=null){
            //4.根据直属领导的上级id将上上级查出来,装到上方集合中
            UserVO auditUSer2 = userMapper.selectById(auditUSer1.getParentId());
            userVOList.add(auditUSer2);
        }
        //5.将准备好的审批人集合返回给controller
        return userVOList;
    }

    @Override
    public UserVO selectById(Long id) {
        log.debug("根据id查询用户业务，参数：{}", id);
        UserVO userVO = userMapper.selectById(id);
        return userVO;
    }
}
